home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / -archivi / -recent2 / gaplib.lha / GAPLib_Beta / examples / tutorial.c < prev   
C/C++ Source or Header  |  1999-01-29  |  1KB  |  71 lines

  1. /*
  2.  * GAP-Lib tutorial source.
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <GAP.h>
  8. #include <math.h>
  9. #include <limits.h>
  10. #include <time.h>
  11.  
  12. struct Polyphant {
  13.    unsigned long value;
  14. };
  15.  
  16. void init(struct Polyphant *);
  17. double fitfunc(struct Polyphant *);
  18.  
  19. #ifndef    PI
  20. #define    PI    3.14159265
  21. #endif
  22.  
  23. int main(void)
  24. {
  25. int i;
  26. struct Population *Pop;
  27. struct Polyphant *Individual;
  28. struct TagItem EvolveTags[]={
  29.    {EVL_Evaluator,(IPTR)fitfunc},
  30.    {TAG_DONE,0L}
  31. };
  32.  
  33.  
  34. EnterGAP(2);
  35.  
  36. InitRand(time(NULL));
  37.  
  38. Pop = CreatePopulationT(20,sizeof(struct Polyphant),POP_Init,init,TAG_DONE);
  39.  
  40. for(i=0;i!=50;i++) {
  41.    Pop = Evolve(Pop,EvolveTags);
  42.    printf("Generation %d: Max = %lf\n",i+1,Pop->Stat.MaxFitness);
  43. }
  44.  
  45. Individual = Pop->Stat.Max;
  46.  
  47. printf("After %d generations:\n",i);
  48. printf("Best value = %lf.\n",Pop->Stat.MaxFitness);
  49. printf("For f(%lf).\n",IRange(Individual->value,0.0,PI));
  50.  
  51. DeletePopulation(Pop);
  52.  
  53. return(0);
  54. }
  55.  
  56. void init(struct Polyphant *Polly)
  57. {
  58. /* Rnd() only returns values between 0 and max 2147483646 (30 bits) */
  59. Polly->value = Rnd(0x7ffffffe)^(Rnd(0x7ffffffe)<<2);
  60. }
  61.  
  62. double fitfunc(struct Polyphant *Polly)
  63. {
  64. double x;
  65. x = IRange(Polly->value,0.0,PI);
  66. return(x+sin(32*x));
  67. }
  68.  
  69.  
  70.  
  71.